package com.sihan.framework.etm.controller.business;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.framework.annotation.BusinessLog;
import com.framework.exception.BusinessException;
import com.framework.model.ApiResult;
import com.sihan.framework.etm.common.util.excel.ExcelUtils;
import com.sihan.framework.etm.dto.DeviceExcelDTO;
import com.sihan.framework.etm.service.baseData.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;

import com.sihan.framework.etm.service.business.ExcelService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
@Slf4j
@Api(tags = "excel")
@RestController
@RequestMapping("/bs/v0/excel")
public class ExcelController {
	
	@Autowired
	private ExcelService excelService;

	@Autowired
	private DeviceService deviceService;

	@PostMapping("/tool-import")
	@ApiOperation(value = "工器具信息导入")
	public ApiResult<?> cardImport(@RequestPart("file") MultipartFile file) throws Exception {
		List<DeviceExcelDTO> list = ExcelUtils.readMultipartFile(file, DeviceExcelDTO.class);
		return deviceService.saveOrDeviceExcelDTO(list);
	}

	@GetMapping("/plan-detail")
    @ApiOperation(value = "计划详情导出")
	public void downloadPlanDetail(HttpServletRequest request,HttpServletResponse response, String planId) {
		excelService.downloadPlanDetail(request, response, planId);
	}
	
	@GetMapping("/tool-standingbook")
    @ApiOperation(value = "工器具台账导出")
	public void downloadToolSstandingbook(HttpServletRequest request,HttpServletResponse response, String typeId) {
		excelService.downloadToolStandingbook(request, response, typeId);
	}

	@ApiOperation(value = "下载工器具信息导入模版")
	@PostMapping("/load/template")
	@BusinessLog(value = "下载工器具信息导入模版", type = 1)
	public void loadTemplateFile(HttpServletRequest request, HttpServletResponse response, String fileName) {
		BufferedOutputStream bos = null;
		InputStream inputStream = null;
		if (StringUtils.isBlank(fileName)) {
			fileName = "工器具信息导入模版.xls";
		}
		ClassPathResource cpr = new ClassPathResource("excel/" + fileName);
		byte[] buffer = new byte[1024];
		try {
			String ifileName = new String((fileName).getBytes(), "iso8859-1");
			response.setContentType("application/vnd.ms-excel;charset=UTF-8");
			response.setHeader("Content-Disposition", "attachment;filename=" + ifileName);
			response.setCharacterEncoding("utf-8");
			inputStream = cpr.getInputStream();
			bos = new BufferedOutputStream(response.getOutputStream());
			int bytesRead = 0;
			while ((bytesRead = inputStream.read(buffer)) != -1) {
				bos.write(buffer, 0, bytesRead);
			}
		} catch (IOException e) {
			throw new BusinessException("下载模板文件异常", e);
		} finally {
			try {
				if (null != inputStream) {
					inputStream.close();
				}
			} catch (IOException e) {
				log.error("下载模板文件关闭流异常", e);
			}
			try {
				if (bos != null)
					bos.close();
			} catch (IOException e) {
				log.error("下载模板文件关闭流异常", e);
			}
		}
	}

}
